package com.jintoufs.web.action.sys;

import com.alibaba.fastjson.JSON;
import com.jhj.common.util.JhjResponseUtil;
import com.jintoufs.domain.customer.CustomerRole;
import com.jintoufs.domain.role.Role;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.customer.CustomerResourceService;
import com.jintoufs.service.customer.CustomerRoleService;
import com.jintoufs.service.role.RoleService;
import com.jintoufs.service.security.ResourceService;
import com.jintoufs.util.ztree.TreeNode;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
@RequestMapping("/role")
public class RoleAction extends SupportAction {

    @Autowired
    private CustomerRoleService customerRoleService;

    @Autowired
    private CustomerResourceService customerResourceService;

    @RequestMapping("/list")
    @SystemControllerLog(description = "获取角色列表")
    public String list(Model model, String firstInit) {
        List<CustomerRole> list = customerRoleService.getByFilter(null);
        model.addAttribute("listEntity", list);
        model.addAttribute("firstInit", firstInit == null ? "true" : firstInit);
        return "role/list";
    }

    /**
     * 新增保存<br>
     * 页面传回参数：<br>
     *
     * @param record
     * @param response
     */
    @RequestMapping("add")
    @SystemControllerLog(description = "新增角色")
    public void add(CustomerRole record, String resourceIds, HttpServletResponse response) {
        Map<String, Object> result = customerRoleService.add(record, resourceIds);
        JhjResponseUtil.responseJson(response, JSON.toJSONString(result));
    }

    /**
     * input.do?id= 进入类目编辑页面 <br>
     * input.do 进入类目新增页面（不包含选中父类目）
     *
     * @param id    类目Id
     * @param model
     * @return vm/category/input.vm
     */
    @RequestMapping("input")
    public String input(Integer id, Model model) {
        CustomerRole entity = null;

        // 是否为编辑保存
        Boolean isEdit = false;

        if (id != null) {
            // 修改
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", id);
            List<CustomerRole> list = customerRoleService.getByFilter(params);
            if (!list.isEmpty()) {
                entity = list.get(0);
            }
            isEdit = true;
        } else {
            // 新增
        }
        String roleId = "0";

        List<TreeNode> list = customerResourceService.getResourceTreeByRole(id);
        model.addAttribute("listTree", JSON.toJSONString(list));
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("entity", entity);
        return "role/input";
    }

    @RequestMapping("edit")
    @SystemControllerLog(description = "修改角色信息")
    public void edit(CustomerRole role, String resourceIds, HttpServletResponse response) {
        Map<String, Object> result = customerRoleService.update(role, resourceIds);
        JhjResponseUtil.responseJson(response, JSON.toJSONString(result));
    }

    @RequestMapping("delete")
    @SystemControllerLog(description = "删除角色")
    public void delete(HttpServletResponse response, Integer[] id) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            for (Integer _id : id) {
                if (_id != null && _id != 0) {
                    customerRoleService.delete(_id);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof DataIntegrityViolationException) {
                ajaxMsg.setCode("500");
                ajaxMsg.setMessage("角色还在使用中，删除失败！");
            }
        }
        writeInfo(response, JSON.toJSONString(ajaxMsg));
    }
}
